home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / SCROLL_B / SCROLL_B.C
C/C++ Source or Header  |  1990-10-10  |  11KB  |  273 lines

  1. /*
  2.  
  3.     File:        Scroll Bar.c
  4.     Author:        Dana Basken
  5.     Date:        October 10, 1990 (hey, the shuttle just landed!)
  6.                 (text entered at Monaco 9, tabs are 4 spaces)
  7. */
  8. /************************************************************************/
  9. /*    demo of text help routine.                                            */
  10. /************************************************************************/
  11.  
  12.  
  13. ControlHandle        myControl,hitControlHdl;
  14. WindowPtr            myWindow,hitWindow;
  15. Rect                myRect,textRect,scrollRect;
  16. int                    programOver,hitControl,dummy,currentValue,minValue,maxValue,oldValue;
  17. EventRecord            myEvent;
  18. Handle                myText;
  19. TEHandle            myTE;
  20.  
  21. /************************************************************************/
  22. /*    initialize()                                                        */
  23. /*                                                                        */
  24. /*        standard initialization                                            */
  25. /*        initialize help variables as well                                */
  26. /*                                                                        */
  27. /************************************************************************/
  28. void initialize()    {
  29.     /************************************************************/
  30.     /*    initialize everything as normal:                        */
  31.     /************************************************************/
  32.     MaxApplZone();
  33.     InitGraf(&thePort);
  34.     InitFonts();
  35.     FlushEvents(everyEvent, 0);
  36.     InitWindows();
  37.     InitMenus();
  38.     TEInit();
  39.     InitDialogs(0L);
  40.     InitCursor();
  41.     /************************************************************/
  42.     /*    get the help text from the resource file:                */
  43.     /************************************************************/
  44.     myText = GetResource('TEXT',128);
  45.     MoveHHi(myText);
  46.     HLock(myText);
  47.     /************************************************************/
  48.     /*    textRect is the rectangle that outlines the text area    */
  49.     /*    scrollRect is the actual text outline (1 pixel in from    */
  50.     /*        textRect                                            */
  51.     /*    myRect is the rectangle for the scroll bar                */
  52.     /************************************************************/
  53.     SetRect(&textRect,27,10,314,190);
  54.     SetRect(&scrollRect,28,11,313,189);
  55.     SetRect(&myRect,10,10,26,190);
  56.     /************************************************************/
  57.     /*    open up myWindow from resource file and set it to be the*/
  58.     /*    current grafport.  Set the font to be Monaco 9pt, frame    */
  59.     /*    the text area.                                            */
  60.     /************************************************************/
  61.     myWindow = GetNewWindow(128,0L,-1L);
  62.     SetPort(myWindow);
  63.     TextFont(monaco);
  64.     TextSize(9);
  65.     FrameRect(&textRect);
  66.     /************************************************************/
  67.     /*    open a TextEdit record for the help text and set the    */
  68.     /*    text in the record to be our help text resource.        */
  69.     /************************************************************/
  70.     myTE = TENew(&scrollRect,&scrollRect);
  71.     TESetText((*myText),SizeResource(myText),myTE);
  72.     /************************************************************/
  73.     /*    re-calculate the TextEdit record so we get an accurate    */
  74.     /*    count of the number of text lines inside our scrollRect    */
  75.     /*    rectangle.                                                */
  76.     /************************************************************/
  77.     TECalText(myTE);
  78.     /************************************************************/
  79.     /*    create the scroll bar control.  set the current value to*/
  80.     /*    be 1, the low value to be 1 and the high value should be*/
  81.     /*    set to last line in the text edit record minus the        */
  82.     /*    number of lines that will fit in the rectangle (looks    */
  83.     /*    much nicer that way).                                    */
  84.     /************************************************************/
  85.     myControl = NewControl(myWindow,&myRect,"\p",1,1,1,
  86.         (**myTE).nLines-((scrollRect.bottom-scrollRect.top)/(**myTE).lineHeight)+1,
  87.         scrollBarProc,0L);
  88. }
  89.  
  90.  
  91. /************************************************************************/
  92. /*    actionProc()                                                        */
  93. /*                                                                        */
  94. /*        handles the all actions from the TrackControl excluding the        */
  95. /*        inThumb routine (handled later).                                */
  96. /************************************************************************/
  97. pascal void actionProc(actControl,actPart)
  98. ControlHandle    actControl;
  99. int                actPart;    {
  100.  
  101.     /************************************************************/
  102.     /*    hold on to the minimum and maximum values for this        */
  103.     /*    control, as well as the current value (also saved as the*/
  104.     /*    oldValue).                                                */
  105.     /************************************************************/
  106.     minValue = GetCtlMin(actControl);
  107.     maxValue = GetCtlMax(actControl);
  108.     currentValue = GetCtlValue(actControl);
  109.     oldValue = currentValue;
  110.     /************************************************************/
  111.     /*    which part of the scroll bar was clicked in?            */
  112.     /************************************************************/
  113.     switch (actPart)    {
  114.         /************************************************************/
  115.         /*    was it the up-arrow button?                                */
  116.         /************************************************************/
  117.         case inUpButton:
  118.             /************************************************************/
  119.             /*    if the current value is not already at the top, then:    */
  120.             /************************************************************/
  121.             if (currentValue!=minValue)    {
  122.                 SetCtlValue(actControl,currentValue-1);    /* move the control up 1 */
  123.                 TEScroll(0,(**myTE).lineHeight,myTE);    /* move the text down 1 line */
  124.             }
  125.             break;
  126.         /************************************************************/
  127.         /*    was it the down-arrow button?                            */
  128.         /************************************************************/
  129.         case inDownButton:
  130.             /************************************************************/
  131.             /*    if the current value is not already at the bottom, then:*/
  132.             /************************************************************/
  133.             if (currentValue!=maxValue)    {
  134.                 SetCtlValue(actControl,currentValue+1);    /* move the control down 1 */
  135.                 TEScroll(0,-(**myTE).lineHeight,myTE);    /* move the text up 1 line */
  136.             }
  137.             break;
  138.         /************************************************************/
  139.         /*    was it in the scroll region above the thumb?            */
  140.         /************************************************************/
  141.         case inPageUp:
  142.             /************************************************************/
  143.             /*    attempt to move the control up 1/10 of the total text    */
  144.             /*    if the control is too low, set it to the minimum value    */
  145.             /*    now, move the control.                                    */
  146.             /*    scroll the text by the offset of the old value-new        */
  147.             /************************************************************/
  148.             currentValue -= ((maxValue-minValue)/10);
  149.             if (currentValue < minValue) {currentValue = minValue;}
  150.             SetCtlValue(actControl,currentValue);
  151.             TEScroll(0,(**myTE).lineHeight*(oldValue-currentValue),myTE);
  152.             break;
  153.         /************************************************************/
  154.         /*    was it in the scroll region below the thumb?            */
  155.         /************************************************************/
  156.         case inPageDown:
  157.             /************************************************************/
  158.             /*    attempt to move the control down 1/10 of the total text    */
  159.             /*    if the control is too high, set it to the maximum value    */
  160.             /*    now, move the control.                                    */
  161.             /*    scroll the text by the offset of the old value-new        */
  162.             /************************************************************/
  163.             currentValue += ((maxValue-minValue)/10);
  164.             if (currentValue > maxValue) {currentValue = maxValue;}
  165.             SetCtlValue(actControl,currentValue);
  166.             TEScroll(0,(**myTE).lineHeight*(oldValue-currentValue),myTE);
  167.             break;
  168.     }
  169. }
  170.  
  171.  
  172. /************************************************************************/
  173. /*    main()                                                                */
  174. /*                                                                        */
  175. /*        initializes program and handles events                            */
  176. /*                                                                        */
  177. /************************************************************************/
  178. void main()    {
  179.     initialize();
  180.     programOver = 0;
  181.     do    {
  182.         SystemTask();
  183.         /************************************************************/
  184.         /*    use WaitNextEvent for MultiFinder, or switch it to        */
  185.         /*    GetNextEvent if you want.                                */
  186.         /************************************************************/
  187.         if (WaitNextEvent(everyEvent,&myEvent,15,0L))    {
  188.             switch (myEvent.what)    {
  189.                 case updateEvt:
  190.                     BeginUpdate(myEvent.message);
  191.                     DrawControls(myEvent.message);    /* make sure control is visible    */
  192.                     EraseRect(&textRect);
  193.                     TEUpdate(&scrollRect,myTE);        /* update the help text            */
  194.                     FrameRect(&textRect);            /* draw frame around text        */
  195.                     EndUpdate(myEvent.message);
  196.                     break;
  197.                 case activateEvt:
  198.                     if ((myEvent.modifiers&activeFlag)!=0) {
  199.                         HiliteControl(myControl,0);        /* activate: control is on    */
  200.                     } else {
  201.                         HiliteControl(myControl,255);    /* deactiviate: control is off */
  202.                     }
  203.                     break;
  204.                 case app4Evt:
  205.                     if (myEvent.message & 1L) {        /* resume event */
  206.                         HiliteControl(myControl,0);
  207.                     } else {                        /* suspend event */
  208.                         HiliteControl(myControl,255);
  209.                     }
  210.                     break;
  211.                 case keyDown:
  212.                 case autoKey:
  213.                     break;        /* do nothing in this demo */
  214.                 case mouseDown:
  215.                     /************************************************************/
  216.                     /*    when the mouse is clicked, find out what it was clicked    */
  217.                     /*    on.  we handle the control only on a click in the         */
  218.                     /*    content region:                                            */
  219.                     /************************************************************/
  220.                     switch (FindWindow(myEvent.where,&hitWindow))    {
  221.                         case inContent:
  222.                             /************************************************************/
  223.                             /*    change the mouse coordinates to local, then see which    */
  224.                             /*    part of the control (if any) was clicked on.  if the    */
  225.                             /*    mouse was clicked on the up or down buttons or in the    */
  226.                             /*    scroll region, we'll use our action procedure defined    */
  227.                             /*    above to handle it, otherwise (ie. the thumb was clicked*/
  228.                             /*    on) we handle it here.                                    */
  229.                             /************************************************************/
  230.                             GlobalToLocal(&myEvent.where);
  231.                             hitControl=FindControl(myEvent.where,myWindow,&hitControlHdl);
  232.                             if (hitControlHdl == myControl)    {
  233.                                 switch (hitControl)    {
  234.                                     case inUpButton:
  235.                                     case inDownButton:
  236.                                     case inPageUp:
  237.                                     case inPageDown:
  238.                                         dummy = TrackControl(hitControlHdl,myEvent.where,&actionProc);
  239.                                         break;
  240.                                     case inThumb:
  241.                                         /************************************************************/
  242.                                         /*    similar to in the action procedure above, this section    */
  243.                                         /*    will get the old value before calling TrackControl and    */
  244.                                         /*    the new value after TrackControl.  The difference from    */
  245.                                         /*    old to current value is the number of lines to shift the*/
  246.                                         /*    text.  Note that if no movement of the thumb took place,*/
  247.                                         /*    we don't move anything.                                    */
  248.                                         /************************************************************/
  249.                                         oldValue = GetCtlValue(hitControlHdl);
  250.                                         dummy = TrackControl(hitControlHdl,myEvent.where,0L);
  251.                                         currentValue = GetCtlValue(hitControlHdl);
  252.                                         if (oldValue!=currentValue)    {
  253.                                             TEScroll(0,(**myTE).lineHeight*(oldValue-currentValue),myTE);
  254.                                         }
  255.                                         break;
  256.                                 }
  257.                             }
  258.                             break;
  259.                         case inGoAway:
  260.                             if (TrackGoAway(hitWindow,myEvent.where)) {programOver=1;}
  261.                             break;
  262.                         case inDrag:
  263.                             DragWindow(hitWindow,myEvent.where,&screenBits.bounds);
  264.                             break;
  265.                     }
  266.                     break;
  267.             }
  268.         }
  269.     } while (programOver==0);
  270.     HUnlock(myText);
  271.     DisposHandle(myText);
  272.     DisposeWindow(myWindow);
  273. }